using System.Text;
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using BA.DAL;
using BA;

namespace BlazeApps.Library.Polls
{
    [Serializable()]
    public class PollVoteCheck : Csla.ReadOnlyBase<PollVoteCheck>
    {
        private BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();

        #region Business Properties and Methods

        //declare members
        private Guid _pollId = Guid.Empty;
        private Guid _userid = Guid.Empty;
        private Boolean _hasvoted = false;

        [System.ComponentModel.DataObjectField(true, false)]
        public Guid PollId
        {
            get
            {
                CanReadProperty("PollId", true);
                return _pollId;
            }
        }

        [System.ComponentModel.DataObjectField(true, false)]
        public Guid Userid
        {
            get
            {
                CanReadProperty("Userid", true);
                return _userid;
            }
        }

        public Boolean HasVoted
        {
            get
            {
                return _hasvoted;
            }
        }

        protected override object GetIdValue()
        {
            return string.Format("{0}:{1}", _pollId, _userid);
        }

        #endregion //Business Properties and Methods

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in PollVoteCheck
            //AuthorizationRules.AllowRead("PollId", "PollVoteCheckReadGroup");
            //AuthorizationRules.AllowRead("PollChoice", "PollVoteCheckReadGroup");
            //AuthorizationRules.AllowRead("Userid", "PollVoteCheckReadGroup");

        }


        public static bool CanGetObject()
        {
            //TODO: Define CanGetObject permission in PollVoteCheck
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("PollVoteCheckViewGroup"))
            //	return true;
            //return false;
        }
        #endregion //Authorization Rules

        #region Factory Methods
        private PollVoteCheck()
        { /* require use of factory method */
        }

        public static PollVoteCheck GetPollVoteCheck(Guid pollId, Guid userid)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a PollVoteCheck");
            return DataPortal.Fetch<PollVoteCheck>(new Criteria(pollId, userid));
        }
        #endregion //Factory Methods

        #region Data Access

        #region Criteria

        [Serializable()]
        private class Criteria
        {
            public Guid PollId;
            public Guid Userid;

            public Criteria(Guid pollId, Guid userid)
            {
                this.PollId = pollId;
                this.Userid = userid;
            }
        }

        #endregion //Criteria

        #region Data Access - Fetch
        private void DataPortal_Fetch(Criteria criteria)
        {
            ExecuteFetch(criteria);
        }

        private void ExecuteFetch(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            DAL.AddParameter("@PollId", criteria.PollId);
            DAL.AddParameter("@userid", criteria.Userid);

            using(SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.AllowUserToVote.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
            {
                FetchObject(dr);

                //load child object(s)
                FetchChildren(dr);
            }
        }

        private void FetchObject(SafeDataReader dr)
        {
            if (dr.Read())
            {
                _hasvoted = true;
            }
            else
            {
                _hasvoted = false;
            }
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch
        #endregion //Data Access
    }
}
